package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.Keg;

public class KegPresenter extends AbstractPresenter<Keg> {
	final String SINGLEITEM = "scanTblKeg";
	final String MULTIITEM = "scanTblKegs";
	
	public ArrayList<Keg> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Keg tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Keg createItemFromJSONObject(JSONObject jsonObject){
		Keg item = new Keg();
		try{
			item.setKegID(jsonObject.getInt("kegID"));
			item.setKegType(jsonObject.getString("kegType"));
			item.setKegBatch(jsonObject.getInt("kegBatch"));
			item.setKegLost(jsonObject.getBoolean("kegLost"));
			item.setKegOOC(jsonObject.getBoolean("kegOOC"));
			item.setScanTime(timeFormat.parse(jsonObject.getString("scanTime")));
			
			final PackageTypePresenter ptPresenter = new PackageTypePresenter();
			JSONObject jsonPT = jsonObject.getJSONObject("kegPackageID");
			item.setPackageID(ptPresenter.createItemFromJSONObject(jsonPT));			
			
			if(!jsonObject.isNull("kegBeer")){
				POSBeerViewPresenter presenter = new POSBeerViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("kegBeer");
				item.setKegBeer(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setKegBeer(null);
			}		
			
			if(!jsonObject.isNull("kegCust")){
				CustomerPresenter presenter = new CustomerPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("kegCust");
				item.setKegCust(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setKegCust(null);
			}	
			
			if(!jsonObject.isNull("dateSold")){
				item.setDateSold(timeFormat.parse(jsonObject.getString("dateSold")));
			} else {
				item.setDateSold(null);
			}	
			
			if(!jsonObject.isNull("kegFGI")){
				FGIPresenter presenter = new FGIPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("kegFGI");
				item.setKegFGI(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setKegFGI(null);
			}	
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Keg item){
		try{
			JSONObject json = new JSONObject();
			json.put("kegID", item.getKegID());
			json.put("kegType", item.getKegType());
			json.put("kegBatch", item.getKegBatch());
			json.put("kegLost", item.isKegLost());
			json.put("kegOOC", item.isKegOOC());
			json.put("scanTime", getDate(item.getScanTime()));
			
			final PackageTypePresenter ptPresenter = new PackageTypePresenter();
			json.put("kegPackageID", ptPresenter.createJSON(item.getPackageID()));
			
			if(item.getKegBeer()!=null){
				POSBeerViewPresenter presenter = new POSBeerViewPresenter();
				json.put("kegBeer", presenter.createJSON(item.getKegBeer()));
			} else {
				json.put("kegBeer", JSONObject.NULL);
			}

			if(item.getKegCust()!=null){
				CustomerPresenter presenter = new CustomerPresenter();
				json.put("kegCust", presenter.createJSON(item.getKegCust()));
			} else {
				json.put("kegCust", JSONObject.NULL);
			}
			
			if(item.getDateSold()!=null){
				json.put("dateSold", getDate(item.getDateSold()));
			} else {
				json.put("dateSold", JSONObject.NULL);
			}
			
			if(item.getKegFGI()!=null){
				FGIPresenter presenter = new FGIPresenter();
				json.put("kegFGI", presenter.createJSON(item.getKegFGI()));
			} else {
				json.put("kegFGI", JSONObject.NULL);
			}

			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Keg getEmptyEntity(){
		Keg entity = new Keg();
		return entity;
	}
}
